//先加载页面完成
$(function(){
    layui.use(['form'], function() {
        var form = layui.form;
        var layer = layui.layer;
        // 准备工作
        $(".headline").each(function () {
            $(this).next(".collapse").collapse('show');
            $(this).find(".headline-status img").attr("src", "img/minus.png");
        });

        $(".headline").on("click", function () {
            if ($(this).next(".collapse").is(":hidden")) {
                $(this).next(".collapse").css({"display": "block"})
                $(this).next(".collapse").collapse('toggle');
                $(this).find(".headline-status img").attr("src", "img/minus.png");
            } else {
                $(this).next(".collapse").css({"display": "none"})
                $(this).next(".collapse").collapse('toggle');
                $(this).find(".headline-status img").attr("src", "img/add.png");
            }
        });

        // 声明全局变量
        var variableArray = [];
        variableArray = $(".variableDeclarations .variable");
        variableArray.each(function (){
            var variableName = $(this).find(".variableName").html().trim();
            // 是否存在初始化变量值表达式
            if ($(this).find(".expression").length > 0) {
                value = $(this).find(".expression").text().trim();
                window[variableName] = value;
            } else {
                window[variableName];
            }
        });

        //区分DM
        var layerDM = 1;
        var dmSeqLengthFlag = 1;
        //初始化显示第一个过程类dmNode内容
        firstShowDmNodes();
        function firstShowDmNodes() {
            //删除页面最下方按钮
            $(".process>.btn-step").remove();
            $(".process .title").addClass("hidden");
            var dmSeqLength = $(".process>.dmSeq").children().length;
            //循环页面元素
            $(".process>.dmSeq").children().each(function(indexs){
                //判断页面里的dmNode
                if($(this).hasClass("dmNode")){
                    var nub = $(this).find(".proceduralStep").length;
                    var nubIndexs = indexs;
                    if($(this).find(".proceduralStep").length > 0){
                        // 删除第一个顺序执行方式的dmNodes标签的hidden
                        $(this).removeClass("hidden");
                        // 循环所有dmNodes标签，显示第一个顺序执行的内容
                        var steps = $(this).find(".proceduralStep");
                        $(steps).removeClass("hidden");
                        steps.each(function(index){
                            if(index == 0 && indexs == 0){
                                var html = `
                                <div class="btn-step">
                                    <div class="next btn-group" style="display: inline-block">
                                        <button type="button" class="layui-btn layui-btn-normal" onclick="sequenceOrLoopNext(`+nub+`,`+nubIndexs+`,`+dmSeqLength+`)">下一步</button>
                                    </div>
                                </div>
                                <div class="bottom-line_fault"></div>`;
                                $($(this).find(".para")[0]).append(html);
                            }else{
                                var html = `
                                <div class="btn-step">
                                    <div class="next btn-group" style="display: inline-block">
                                        <button type="button" class="layui-btn layui-btn-normal" onclick="sequenceOrLoopNext(`+nub+`,`+nubIndexs+`,`+dmSeqLength+`)">下一步</button>
                                    </div>
                                </div>
                                <div class="bottom-line_fault"></div>`;
                                $($(this).find(".para")[0]).append(html);
                                $($(this).find(".para")[0]).css("color","#999999");
                                $($(this).find(".para")[0]).find("button").css("border","1px solid #CCCCCC");
                                $($(this).find(".para")[0]).find("button").attr("disabled","true");
                                $($(this).find(".para")[0]).find("button").addClass("layui-disabled");
                            }
                        })
                    //判断页面里的表单
                    }else if($(this).find(".dialog").length > 0){
                        //普通顺序执行
                        var dmNodeAll = $($(".dmSeq")[0]).children();
                        dialog($(this),$(dmNodeAll[dmSeqLength-1]));
                        $(this).removeClass("hidden");
                        $(this).find(".dialog").addClass("hidden");
                        $(this).find("from").css("color","#999999");
                        $(this).find("button").css("border","1px solid #CCCCCC");
                        $(this).find("button").attr("disabled","true");
                        $(this).find("button").addClass("layui-disabled");
                    }
                //判断页面里的条件
                }else if($(this).hasClass("dmIf")){
                    // console.log($(this).find(".dmSeq .dmNode"))
                    $(this).find(".dmSeq .dmNode").removeClass("hidden");
                    $(this).find(".dmSeq .dmNode .proceduralStep").removeClass("hidden");
                    // 循环所有dmNodes标签，显示第一个顺序执行的内容
                    var steps = $(this).find(".dmSeq .dmNode");
                    //$(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq")
                    steps.each(function(index){
                        var nub = $(this).find(".proceduralStep").length;
                        var nubIndexs = index;
                        if($(this).find(".proceduralStep").length > 0){
                            var html = `
                            <div class="btn-step">
                                <div class="next btn-group" style="display: inline-block">
                                    <button type="button" class="layui-btn layui-btn-normal" onclick="sequenceOrLoopNext(`+nub+`,`+nubIndexs+`,`+dmSeqLength+`)">下一步</button>
                                </div>
                            </div>
                            <div class="bottom-line_fault"></div>`;
                            $(this).find(".para").append(html);
                            $(this).find(".para").css("color","#999999");
                            $(this).find(".para").find("button").css("border","1px solid #CCCCCC");
                            $(this).find(".para").find("button").attr("disabled","true");
                            $(this).find(".para").find("button").addClass("layui-disabled");
                        }else if($(this).find(".dialog").length > 0){
                            //普通顺序执行
                            var dmNodeAll = $($(".dmSeq")[0]).children();
                            dialog2($(this),$(dmNodeAll[dmSeqLength-1]));
                            $(this).removeClass("hidden");
                            $(this).find(".dialog").addClass("hidden");
                            $(this).find("from").css("color","#999999");
                            $(this).find("button").css("border","1px solid #CCCCCC");
                            $(this).find("button").attr("disabled","true");
                            $(this).find("button").addClass("layui-disabled");
                        }else{
                            var html = `
                            <div class="btn-step">
                                <div class="next btn-group" style="display: inline-block">
                                    <button type="button" class="layui-btn layui-btn-normal" onclick="sequenceOrLoopNext(`+0+`,`+nubIndexs+`,`+dmSeqLength+`)">下一步</button>
                                </div>
                            </div>
                            <div class="bottom-line_fault"></div>`;
                            $(this).append(html)
                            $(this).find("button").css("border","1px solid #CCCCCC");
                            $(this).find("button").attr("disabled","true");
                            $(this).find("button").addClass("layui-disabled");
                        }
                    })
                }
            });
        }
        //下一步点击次数
        var current = 0;
        //执行顺序的区分判断--Next
        sequenceOrLoopNext = function(nub,nubIndexs){
            if($(this).hasClass("layui-disabled")) return;
            if (layerDM == 1) {
                //普通顺序执行
                var dmNodeAll = $(".process>.dmSeq").children();
                current += 1;
                if(parseInt(nub)==0) nub+=1;
                //循环所有dmNode标签
                $.each(dmNodeAll, function (index, value) {
                    if (!$(this).hasClass("hidden")) {
                        //修改上一步步骤样式
                        if(index==nubIndexs){
                            //最后一个
                            if (current == parseInt(nub)) {
                                var newIndex = (parseInt(nubIndexs) + 1);
                                // 该dmNode下最后一个proceduralStep或者没有子步骤,点击就是进入下一个顺序执行方式
                                if($(dmNodeAll[newIndex]).length>0){
                                    $($(this).find(".para")[current-1]).css("color","#999999");
                                    $($(this).find(".para")[current-1]).find("button").css("border","1px solid #CCCCCC");
                                    $($(this).find(".para")[current-1]).find("button").attr("disabled","true");
                                    $($(this).find(".para")[current-1]).find("button").addClass("layui-disabled");
                                    dmSeqLengthFlag = (parseInt(dmSeqLengthFlag) + 1)
                                    $(dmNodeAll[newIndex]).find(".para,from").css("color","#333333");
                                    $(dmNodeAll[newIndex]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                    $(dmNodeAll[newIndex]).find("button").removeAttr("disabled");
                                    $(dmNodeAll[newIndex]).find("button").removeClass("layui-disabled");
                                    current = 0;
                                    $("html,body").animate({scrollTop:$(dmNodeAll[newIndex]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                                }else{
                                    //最后一个板块--最后一个下一步按钮
                                    current = current;
                                    var dmNodeEle = $($(dmNodeAll)[parseInt(dmSeqLengthFlag)-1]);
                                    $($(dmNodeEle).find(".para")[current-1]).css("color","#333333");
                                    $($(dmNodeEle).find(".para")[current-1]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                    $($(dmNodeEle).find(".para")[current-1]).find("button").removeAttr("disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".back>button").removeClass("layui-disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".back>button").removeAttr("disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".back>button").removeClass("layui-disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".next>button").css("border","1px solid #CCCCCC");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".next>button").attr("disabled","true");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".next>button").addClass("layui-disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".next>button").addClass("layui-disabled");
                                    $($(dmNodeEle).find(".para")[current-1]).find(".next>button").text("完成");
                                    layer.msg("这是最后一步");
                                    $("html,body").animate({scrollTop:$($(dmNodeEle).find(".para")[current-1]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                                }
                            }else{
                                $($(this).find(".para")[current-1]).css("color","#999999");
                                $($(this).find(".para")[current-1]).find("button").css("border","1px solid #CCCCCC");
                                $($(this).find(".para")[current-1]).find("button").attr("disabled","true");
                                $($(this).find(".para")[current-1]).find("button").addClass("layui-disabled");
                                //修改下一步步骤样式
                                $($(this).find(".para")[current]).css("color","#333333");
                                $($(this).find(".para")[current]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                $($(this).find(".para")[current]).find("button").removeAttr("disabled");
                                $($(this).find(".para")[current]).find("button").removeClass("layui-disabled");
                                $("html,body").animate({scrollTop:$($(this).find(".para")[current]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                            }
                        }
                    }
                });
            }else if (layerDM == 2) {
                //循环顺序执行
                var dmNodeAll = $(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq").children(".dmNode");
                var dmNodeAllChildern = $(dmNodeAll).find(".proceduralStep,a,.dialog")
                var dmSeqLength = $(".process>.dmSeq").children().length;
                if(current == "wrongAnswer"){
                    wrongAnswerFn();
                    $.each(dmNodeAll, function (index, value) {
                        if($(this).attr("applicrefid")=="wrongAnswer"){
                            $(this).find(".para").css("color","#999999");
                            $(this).find(".para").find("button").css("border","1px solid #CCCCCC");
                            $(this).find(".para").find("button").attr("disabled","true");
                            $(this).find(".para").find("button").addClass("layui-disabled");
                        }
                    })
                    return;
                }else if(current == "rightAnswer"){
                    var dmNodeChildren = $($(".dmSeq")[0]).children();
                    rightAnswerFn($(dmNodeChildren[dmSeqLength-1]));
                    $.each(dmNodeAll, function (index, value) {
                        if($(this).attr("applicrefid")=="rightAnswer"){
                            $(this).find(".para").css("color","#999999");
                            $(this).find(".para").find("button").css("border","1px solid #CCCCCC");
                            $(this).find(".para").find("button").attr("disabled","true");
                            $(this).find(".para").find("button").addClass("layui-disabled");
                        }
                    })
                    return;
                }else if($($(dmNodeAllChildern)[current]).hasClass("proceduralStep")){
                    $($(dmNodeAllChildern)[current]).find(".para").css("color","#999999");
                    $($(dmNodeAllChildern)[current]).find(".para").find("button").css("border","1px solid #CCCCCC");
                    $($(dmNodeAllChildern)[current]).find(".para").find("button").attr("disabled","true");
                    $($(dmNodeAllChildern)[current]).find(".para").find("button").addClass("layui-disabled");
                }else if($($(dmNodeAllChildern)[current]).hasClass("dialog")){
                    // $($(dmNodeAllChildern)[current]).find("from").css("color","#999999");
                    // $($(dmNodeAllChildern)[current]).find("button").css("border","1px solid #CCCCCC");
                    // $($(dmNodeAllChildern)[current]).find("button").attr("disabled","true");
                    // $($(dmNodeAllChildern)[current]).find("button").addClass("layui-disabled");
                }else{
                    // $($(dmNodeAllChildern)[current]).parent().css("color","#333333");
                    $($(dmNodeAllChildern)[current]).parent().find("button").css("border","1px solid #CCCCCC");
                    $($(dmNodeAllChildern)[current]).parent().find("button").attr("disabled","true");
                    $($(dmNodeAllChildern)[current]).parent().find("button").addClass("layui-disabled");
                }
                //修改下一步步骤样式
                if($($(dmNodeAllChildern)[current+1]).hasClass("proceduralStep")){
                    $($(dmNodeAllChildern)[current+1]).find(".para").css("color","#333333");
                    $($(dmNodeAllChildern)[current+1]).find(".para").find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                    $($(dmNodeAllChildern)[current+1]).find(".para").find("button").removeAttr("disabled");
                    $($(dmNodeAllChildern)[current+1]).find(".para").find("button").removeClass("layui-disabled");
                    $("html,body").animate({scrollTop:$($(dmNodeAllChildern)[current+1]).find(".para").offset().top - $(window).height()/2 + 100 + "px"}, 500);
                }else if($($(dmNodeAllChildern)[current+1]).hasClass("dialog")){
                    $($(dmNodeAllChildern)[current+1]).next().css("color","#333333");
                    $($(dmNodeAllChildern)[current+1]).next().find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                    $($(dmNodeAllChildern)[current+1]).next().find("button").removeAttr("disabled");
                    $($(dmNodeAllChildern)[current+1]).next().find("button").removeClass("layui-disabled");
                    $("html,body").animate({scrollTop:$($(dmNodeAllChildern)[current+1]).next().offset().top - $(window).height()/2 + 100 + "px"}, 500);
                }else{
                    // $($(dmNodeAllChildern)[current+1]).parent().css("color","#333333");
                    $($(dmNodeAllChildern)[current+1]).parent().find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                    $($(dmNodeAllChildern)[current+1]).parent().find("button").removeAttr("disabled");
                    $($(dmNodeAllChildern)[current+1]).parent().find("button").removeClass("layui-disabled");
                    $("html,body").animate({scrollTop:$($(dmNodeAllChildern)[current+1]).next().offset().top - $(window).height()/2 + 100 + "px"}, 500);
                }
                current+=1;
            }
        }
        //上一步按钮点击事件函数
        $(".back").on("click", function () {
            //禁止按钮
	        if($(this).hasClass("layui-disabled")) return;
            //执行顺序的区分判断--Back
            sequenceOrLoopBack();
        })
        //执行顺序的区分判断--Back
        function sequenceOrLoopBack(){
            //普通顺序执行
            if (layerDM == 1) {
                var dmNodeAll = $($(".dmSeq")[0]).children();
                current -= 1;
                if(current == -1){
                        if($(dmNodeAll[dmSeqLengthFlag-1]).hasClass("dmNode")){
                            if($(dmNodeAll[dmSeqLengthFlag-1]).find(".proceduralStep").length > 0){

                            }else if($(dmNodeAll[dmSeqLengthFlag-1]).find(".dialog").length > 0){
                                $(dmNodeAll[dmSeqLengthFlag-1]).find("#dialogForm").css("color","#999999");
                                $(dmNodeAll[dmSeqLengthFlag-1]).find("button").css("border","1px solid #CCCCCC");
                                $(dmNodeAll[dmSeqLengthFlag-1]).find("button").attr("disabled","true");
                                $(dmNodeAll[dmSeqLengthFlag-1]).find("button").addClass("layui-disabled");
                            }
                        }else if($(dmNodeAll[dmSeqLengthFlag-1]).hasClass("dmIf")){
        
                        }
                        if($(dmNodeAll[dmSeqLengthFlag-2]).hasClass("dmNode")){
                            if($(dmNodeAll[dmSeqLengthFlag-2]).find(".proceduralStep").length > 0){
                                var paraNub = $(dmNodeAll[dmSeqLengthFlag-2]).find(".para").length;
                                current = paraNub-1;
                                var paraEle = $($(dmNodeAll[dmSeqLengthFlag-2]).find(".para")[paraNub-1])
                                $(paraEle).css("color","#333333");
                                $(paraEle).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                $(paraEle).find("button").removeAttr("disabled");
                                $(paraEle).find("button").removeClass("layui-disabled");
                                $("html,body").animate({scrollTop:$(paraEle).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                            }else if($(dmNodeAll[dmSeqLengthFlag-2]).find(".dialog").length > 0){
                                $(dmNodeAll[dmSeqLengthFlag-2]).find("#dialogForm").css("color","#333333");
                                $(dmNodeAll[dmSeqLengthFlag-2]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                $(dmNodeAll[dmSeqLengthFlag-2]).find("button").removeAttr("disabled");
                                $(dmNodeAll[dmSeqLengthFlag-2]).find("button").removeClass("layui-disabled");
                                $("html,body").animate({scrollTop:$(dmNodeAll[dmSeqLengthFlag-2]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                            }
                        }else if($(dmNodeAll[dmSeqLengthFlag-2]).hasClass("dmIf")){
        
                        }
                        dmSeqLengthFlag-=1;
                }else{
                    var stepss = $(dmNodeAll).find(".proceduralStep");
                    var stepChildren = $($(dmNodeAll)[dmSeqLengthFlag-1]).children();
                    if(stepChildren.length>1){
                        if($($(stepChildren)[current]).find(".next>button").text()=="完成"){
                            $($(stepChildren)[current]).find(".next>button").text("下一步");
                        }
                        //修改上一步步骤样式
                        $($(stepChildren)[current+1]).find(".para").css("color","#999999");
                        $($(stepChildren)[current+1]).find(".para").find("button").css("border","1px solid #CCCCCC");
                        $($(stepChildren)[current+1]).find(".para").find("button").attr("disabled","true");
                        $($(stepChildren)[current+1]).find(".para").find("button").addClass("layui-disabled");
                        //修改下一步步骤样式
                        $($(stepChildren)[current]).find(".para").css("color","#333333");
                        $($(stepChildren)[current]).find(".para").find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                        $($(stepChildren)[current]).find(".para").find("button").removeAttr("disabled");
                        $($(stepChildren)[current]).find(".para").find("button").removeClass("layui-disabled");
                        $("html,body").animate({scrollTop:$($(stepChildren)[current]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                    }else{
                        //循环所有dmNode标签
                        $.each(dmNodeAll, function (index, value) {
                            //内容中有几个proceduralStep进行操作
                            // var steps = $(this).find(".proceduralStep");
                            //修改上一步步骤样式
                            $($(stepss).find(".para")[current+1]).css("color","#999999");
                            $($(stepss).find(".para")[current+1]).find("button").css("border","1px solid #CCCCCC");
                            $($(stepss).find(".para")[current+1]).find("button").attr("disabled","true");
                            $($(stepss).find(".para")[current+1]).find("button").addClass("layui-disabled");
                            //修改上一步步骤样式
                            $($(stepss).find(".para")[current]).css("color","#333333");
                            $($(stepss).find(".para")[current]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                            $($(stepss).find(".para")[current]).find("button").removeAttr("disabled");
                            $($(stepss).find(".para")[current]).find("button").removeClass("layui-disabled");
                            $("html,body").animate({scrollTop:$($(stepss).find(".para")[current]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
                        })
                    }

                }
            }else if (layerDM == 2) {
                console.log(99999999)
            }
        }

        // 处理 dmNode 中 对话框
        function dialog(obj,nextIndex) {
            var group = $(obj).find(".dialogGroup");
            var newArray = [];
            newArray = $.grep(group, function (n, i) {
                if ($(group[i]).find(".dialogGroup").length == 0) {
                    return group[i];
                }
            });

            if (newArray.length == 0) {
                return false;
            }

            var dialog = `<from id="dialogForm" class="layui-form">
            <div class="form-div">
            <p class="form-div-p">填写信息</p>
            `;
            newArray.forEach(function (value, index) {
                var children = $(newArray[index]).children();
                for (var i = 0; i < children.length; i++) {
                    var prompt = $(children[i]).children(".prompt").text();

                    if ($(children[i]).hasClass("userEntry")) {
                        var variableRef = $(children[i]).children(".variableRef").text();
                        dialog = dialog + `
                            <div class="layui-form-item">
                                <label class="layui-form-label">${prompt}</label>
                                <input type="text" required lay-verify="required" name=${variableRef}  refValue=${variableRef} class="layui-input entry">
                            </div>                        
                        `;
                    }
                    if ($(children[i]).hasClass("menu")) {
                        dialog = dialog + `<p class="question-content">${prompt}</p>`;
                        var choices = $(children[i]).children(".menuChoice")
                        for (var j = 0; j < choices.length; j++) {
                            var prompt2 = $($(choices).children(".prompt")[j]).text();
                            dialog = dialog + `
                    <input type="radio" name="promptName" class="input-radio" style="display: inline-block;" value="${prompt2}">
                    <span class="input-radio-span">${prompt2}</span>`;
                        }
                    }
                }
            })
            dialog = dialog + `</div>
                    <div class="btn-step">
                        <div class="next btn-group" style="display: inline-block">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="from1" id="submitBtn">下一步</button>
                        </div>
                    </div>
                    <div class="bottom-line_fault"></div>
                </from>`;
            $(obj).append(dialog)
            //顺序弹窗提交事件
            form.on('submit(from1)', function(data) {
                //获取from里的值
                var entries = Object.entries(data.field);
                //name和age赋值
                window.name = data.field.name;
                window.age = data.field.age;
                for (var i = 0; i < entries.length; i++) {
                    var returnList = judgmentValueFn(entries[i],newArray);
                    if(returnList[0] !== "" && returnList[2] == "text"){
                        if (!eval(returnList[0])) {
                            layer.msg(returnList[1]);
                            return;
                        }
                    }else if(returnList[2] == "radio") {
                        // console.log(radioFn(returnList[0],returnList[3],obj))
                        if(returnList[0] !== "" && radioFn(returnList[0],returnList[3],obj)){
                            $(obj).find("from").css("color","#999999");
                            $(obj).find(".btn-step").find("button").css("border","1px solid #CCCCCC");
                            $(obj).find(".btn-step").find("button").attr("disabled","true");
                            $(obj).find(".btn-step").find("button").addClass("layui-disabled");
                            $("#dialogForm :input").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").remove("selected");
                            //进入条件if-then分支
                            ifthenFn($(".dmSeq .dmIf").index());
                        }else {
                            $(obj).find("from").css("color","#999999");
                            $(obj).find(".btn-step").find("button").css("border","1px solid #CCCCCC");
                            $(obj).find(".btn-step").find("button").attr("disabled","true");
                            $(obj).find(".btn-step").find("button").addClass("layui-disabled");
                            $("#dialogForm :input").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").remove("selected");
                            //顺序执行方式
                            sequentialFn(nextIndex);
                        }
                    }
                }

            });

        };

        // 判断输入值进行对应操作
        function judgmentValueFn(entries,newArray) {
            //对from的值进行对比判断,返回拼接信息
            return judgmentAgeFn(entries[0],entries[1],newArray);
        };

        // 表达式拼接函数
        function judgmentAgeFn(entries1,entries2,newArray) {
            var expressionValue = '';
            var errorMessage = '';
            var typeName = '';
            var radioName = '';

            newArray.forEach(function (value, index) {
                var children = $(newArray[index]).children();
                for (var i = 0; i < children.length; i++) {
                    if ($(children[i]).hasClass("userEntry")) {//输入框
                        var variableRef = $(children[i]).children(".variableRef").text();
                        //判断是哪个标签---判断标签里是否有validate的标签
                        if(entries1 == variableRef){
                            if ($(children[i]).children().hasClass("validate")) {
                                //validate标签
                                var validateElement = $(children[i]).children();
                                //整个表达式的标签
                                var expressionElement = $(validateElement).children(".expression");
                                //表达式拼接函数
                                expressionValue = expressionFn(expressionElement,entries1,entries2);
                                //错误信息标签
                                errorMessage = $(validateElement).children(".errorMessage").text();
                                //类型
                                typeName = "text";
                            }
                        }
                    }
                    else if ($(children[i]).hasClass("menu")) {//单选框
                        var menuChoiceChildren = $(children[i]).children(".menuChoice");
                        for (var j = 0; j < menuChoiceChildren.length; j++) {
                            var menuChoice = $(menuChoiceChildren[j]).children(".prompt").text();

                            if( entries2 == menuChoice && $(menuChoiceChildren[j]).children().hasClass("assertion") ){
                                radioName = $(menuChoiceChildren[j]).children(".assertion").children(".variableRef").text();
                                expressionValue = $(menuChoiceChildren[j]).children(".assertion").children(".expression").text();
                                //类型
                                typeName = "radio";
                            }else if (entries2 == menuChoice && $(menuChoiceChildren[j]).children().hasClass("noAssertions")) {
                                expressionValue = window.level;
                                //类型
                                typeName = "radio";
                            }
                        }
                    }
                }
            });
            return [expressionValue,errorMessage,typeName,radioName];
        };

        // 处理 dmNode 中 对话框
        function dialog2(obj,ele) {
            //判断是否有dialogGroup标签
            var group = $(obj).find(".dialogGroup");
            if (group.length > 0) {
                var newArray = [];
                newArray = $.grep(group, function (n, i) {
                    if ($(group[i]).find(".dialogGroup").length == 0) {
                        return group[i];
                    }
                });
            }else {//没有dialogGroup标签
                if ($(obj).find(".menu") && $(obj).children(".dialog").children(".menu")){
                    var dialogEle = $(obj).find(".dialog");
                    var newArray = [];
                    newArray = $.grep(dialogEle, function (n, i) {
                        if ($(dialogEle[i]).find(".dialog").length == 0) {
                            return dialogEle[i];
                        }
                    });
                    var dialog = `<from id="dialogForm2" class="layui-form">`;
                    newArray.forEach(function (value, index) {
                        var children = $(obj).children(".dialog");
                        for (var i = 0; i < children.length; i++) {
                            if ($(children[i]).children().hasClass("menu")) {
                                var prompt = $(children[i]).children(".menu").children(".prompt").text();
                                dialog = dialog + `<p class="question-content">${prompt}</p>`;
                                var choices = $(children[i]).children(".menu").children(".menuChoice")
                                for (var j = 0; j < choices.length; j++) {
                                    var prompt2 = $($(choices).children(".prompt")[j]).text();
                                    dialog = dialog + `
                                    <input type="radio" name="promptName" class="input-radio" style="display: inline-block;" value="${prompt2}">
                                    <span class="input-radio-span">${prompt2}</span>`;
                                }
                            }
                        }
                    })
                    dialog = dialog + `
                    <div class="btn-step">
                        <div class="next btn-group" style="display: inline-block">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="from2" id="submitBtn">下一步</button>
                        </div>
                    </div>
                    <div class="bottom-line_fault"></div>
                    </from>`;
                    $(obj).append(dialog)
                    //循环弹窗提交事件
                    form.on('submit(from2)', function (data) {
                        console.log(data)
                        //获取from里的值
                        var entries = Object.entries(data.field);
                        if(entries==[]){
                            layer.msg("必填项不能为空")
                        }
                        for (var i = 0; i < entries.length; i++) {
                            var returnList = judgmentValueFn2(entries[i],newArray);
                            if(returnList[0] !== "" && returnList[2] == "text"){
                                if (!eval(returnList[0])) {
                                    layer.msg(returnList[1]);
                                    return;
                                }
                            }else if (returnList[0] !== "" && returnList[2] == "radio") {
                                if ( returnList[0] == "true" ){
                                    $(obj).find("from").css("color","#999999");
                                    $(obj).find(".btn-step").find("button").css("border","1px solid #CCCCCC");
                                    $(obj).find(".btn-step").find("button").attr("disabled","true");
                                    $(obj).find(".btn-step").find("button").addClass("layui-disabled");
                                    // $("#dialogForm2 :input").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").remove("selected");
                                    var dmNodeAll = $(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq").children(".dmNode");
                                    $.each(dmNodeAll, function (index, value) {
                                        if($(this).attr("applicrefid")=="rightAnswer"){
                                            $(this).find(".para").css("color","#333333");
                                            $(this).find(".para").find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                            $(this).find(".para").find("button").removeAttr("disabled");
                                            $(this).find(".para").find("button").removeClass("layui-disabled");
                                            $("html,body").animate({scrollTop:$(this).find(".para").offset().top - $(window).height()/2 + 100 + "px"}, 500);
                                        }
                                    })
                                    current = "rightAnswer";
                                }else {
                                    $(obj).find("from").css("color","#999999");
                                    $(obj).find(".btn-step").find("button").css("border","1px solid #CCCCCC");
                                    $(obj).find(".btn-step").find("button").attr("disabled","true");
                                    $(obj).find(".btn-step").find("button").addClass("layui-disabled");
                                    // $("#dialogForm2 :input").not(":button, :submit, :reset, :hidden").val("").removeAttr("checked").remove("selected");
                                    var dmNodeAll = $(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq").children(".dmNode");
                                    $.each(dmNodeAll, function (index, value) {
                                        if($(this).attr("applicrefid")=="wrongAnswer"){
                                            $(this).find(".para").css("color","#333333");
                                            $(this).find(".para").find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                                            $(this).find(".para").find("button").removeAttr("disabled");
                                            $(this).find(".para").find("button").removeClass("layui-disabled");
                                            $("html,body").animate({scrollTop:$(this).find(".para").offset().top - $(window).height()/2 + 100 + "px"}, 500);
                                        }
                                    })
                                    current = "wrongAnswer";
                                }
                            }
                        }
                    });
                }
            }
        };

        // 判断输入值进行对应操作2
        function judgmentValueFn2(entries,newArray) {
            //对from的值进行对比判断,返回拼接信息
            return judgmentAgeFn2(entries[1],newArray);
        };

        // 表达式拼接函数2
        function judgmentAgeFn2(entries,newArray) {
            var expressionValue = '';
            var errorMessage = '';
            var typeName = '';
            var radioName = '';
            newArray.forEach(function (value, index) {
                var children = $(newArray[index]).children();
                for (var i = 0; i < children.length; i++) {
                    if ($(children[i]).hasClass("userEntry")) {//输入框
            //             var variableRef = $(children[i]).children(".variableRef").text();
            //             //判断是哪个标签---判断标签里是否有validate的标签
            //             if(entries1 == variableRef){
            //                 if ($(children[i]).children().hasClass("validate")) {
            //                     //validate标签
            //                     var validateElement = $(children[i]).children();
            //                     //整个表达式的标签
            //                     var expressionElement = $(validateElement).children(".expression");
            //                     //表达式拼接函数
            //                     expressionValue = expressionFn(expressionElement,entries1,entries2);
            //                     //错误信息标签
            //                     errorMessage = $(validateElement).children(".errorMessage").text();
            //                     //类型
            //                     typeName = "text";
            //                 }
            //             }
                    }
                    // else if ($(children[i]).hasClass("menu")) {//单选框
            //             var menuChoiceChildren = $(children[i]).children(".menuChoice");
            //             for (var j = 0; j < menuChoiceChildren.length; j++) {
            //                 var menuChoice = $(menuChoiceChildren[j]).children(".prompt").text();
            //
            //                 if( entries2 == menuChoice && $(menuChoiceChildren[j]).children().hasClass("assertion") ){
            //                     radioName = $(menuChoiceChildren[j]).children(".assertion").children(".variableRef").text();
            //                     expressionValue = $(menuChoiceChildren[j]).children(".assertion").children(".expression").text();
            //                     //类型
            //                     typeName = "radio";
            //                 }else if (entries2 == menuChoice && $(menuChoiceChildren[j]).children().hasClass("noAssertions")) {
            //                     // if($(menuChoiceChildren[j]).children(".noAssertions").text() == ""){}
            //                     expressionValue = window.level;
            //                     //类型
            //                     typeName = "radio";
            //                 }
            //             }
            //         }
                    else if ($(children[i]).hasClass("menu")) {
                        var menuChoiceChildren = $(children[i]).children(".menuChoice");
                        for (var j = 0; j < menuChoiceChildren.length; j++) {
                            var menuChoice = $(menuChoiceChildren[j]).children(".prompt").text();
                            if( entries == menuChoice){
                                var assertionChildren = $(menuChoiceChildren[j]).children(".assertion");
                                radioName = menuChoice;
                                expressionValue = $($(assertionChildren[j])[0]).children(".expression").children("span").text();
                                //类型
                                typeName = "radio";
                            }
                        }
                    }
                }
            });
            return [expressionValue,errorMessage,typeName,radioName];
        };

        // 整个表达式拼接函数(整个表达式标签，输入框的键，输入框的值)
        function expressionFn(element,data1,data2) {
            var operatorValue1 = "";
            var booleanOperator = "";
            var operatorValue2 = "";
            //判断表达式元素长度是3，有的前后两个值
            if($(element).children().length == 3 && $(element).children(".expression").length == 2) {
                //获取前后两个值的元素
                operatorValue1 = expressionValue($(element).children(".expression")[0],data1,data2);
                operatorValue2 = expressionValue($(element).children(".expression")[1],data1,data2);
            }
            //表达式运算符
            if ($(element).children().hasClass("booleanOperator")) {
                booleanOperator = symbolicFn($(element).children(".booleanOperator").text());
            }
            //拼接整个表达式
            var operatorList = operatorValue1 + booleanOperator + operatorValue2;
            return operatorList;
        };

        // 表达式符号前后(对比值或运算值）的函数
        function expressionValue(element,data1,data2) {
            var num = false;
            //判断长度是1还是3
            if( $(element).children().length == 1 ) {
                //判断是XML的引用变量还是整型，然后赋值
                if($(element).children().hasClass("expression") && $(element).children().children().length > 0){
                    if($(element).children().children().hasClass("variableRef") && data1 == $(element).children().children().text()){
                        num = data2;
                    }else {
                        num = $(element).children().children().text();
                    }
                }else if($(element).children().hasClass("expression") && $(element).children().children().length == 0) {
                    num = $(element).children(".expression").text();
                }
            }else if ( $(element).children().length == 3 ) {
                if ($(element).children().hasClass("numberOperation") && $(element).children(".expression").length == 2){
                    var opt1 = "";
                    var opt2 = $(element).children(".numberOperation").text();
                    var opt3 = "";
                    if($(element).children().children().hasClass("variableRef") && data1 == $($(element).children(".expression")[0]).children().text()){
                        opt1 = data2;
                    }else {
                        opt1 = $($(element).children(".expression")[0]).children().text();
                    }
                    if($(element).children().children().hasClass("variableRef") && data1 == $($(element).children(".expression")[1]).children().text()){
                        opt3 = data2;
                    }else {
                        opt3 = $($(element).children(".expression")[1]).text();
                    }
                    num = opt1 + opt2 + opt3;
                }
            }
            return num;
        };

        // 符号函数
        function symbolicFn(opt) {
            var operator = opt;
            switch (operator) {
                case "and":
                    operator = "&&";
                    break;
                case "or":
                    operator = "||";
                    break;
                case "not":
                    operator = "!";
                    break;
                case "+":
                    operator = "+";
                    break;
                case "=":
                    operator = "=";
                    break;
            }
            return operator;
        };

        function operatorFn(expression,data1,data2,data3) {
            var booleanValue = false;
            if(expression){
                if(expression == "!" && data2 == "="){
                    booleanValue = !(data1==data3);
                }
            }else {
                if(data2 == "="){
                    booleanValue = (data1==data3);
                }
            }
            return booleanValue;
        }

        // 分支判断函数---返回boolean值
        function radioFn(data,radioName,obj) {
            var group2 = $(obj).parent().find(".dmIf");
            var newArray2 = [];
            newArray2 = $.grep(group2, function (n, i) {
                if ($(group2[i]).find(".dmIf").length == 0) {
                    return group2[i];
                }
            });
            if (newArray2.length == 0) {
                return false;
            }
            var booleanValue = false;
            newArray2.forEach(function (value, index) {
                var children = $(newArray2[index]).children();
                for (var i = 0; i < children.length; i++) {
                    //判断是否有booleanOperator
                    if ($(children[i]).children().hasClass("booleanOperator")){
                        var booleanOperatorElement = symbolicFn($(children[i]).children(".booleanOperator").text());
                        var expressionElement = $(children[i]).children(".expression").children();
                        if ($(children[i]).hasClass("expression") && expressionElement.length == 3 && $(expressionElement).children().hasClass("variableRef")){
                            //给level赋值
                            window.level = data;
                            var data1 = data;
                            var data2 = $(children[i]).children(".expression").children(".numberOperation").text();
                            var data3 = $($(children[i]).children(".expression").children(".expression")[1]).text();
                            booleanValue = operatorFn(booleanOperatorElement,data1,data2,data3);
                        }
                    }else{
                        if ( $(children[i]).hasClass("expression")) {
                            var expressionElement = $(children[i]).children(".expression").children();
                            if (expressionElement.length == 3 && $(expressionElement).children().hasClass("variableRef")){
                                if(radioName == $(expressionElement).children(".variableRef").text()) {
                                    window.level = data;
                                    var data1 = data;
                                    var data2 = $(children[i]).children(".expression").children(".numberOperation").text();
                                    var data3 = $($(children[i]).children(".expression").children(".expression")[1]).text();
                                    booleanValue = operatorFn(data1,data2,data3);
                                }
                            }
                        }
                    }
                }
            });
            return booleanValue;
        };

        // 进入顺序执行方式函数
        function sequentialFn(obj) {
            layerDM = 1;
            current = 0;
            dmSeqLengthFlag = $(obj).index()+1;
            //修改步骤样式
            $($(obj).find(".para")[0]).css("color","#333333");
            $($(obj).find(".para")[0]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
            $($(obj).find(".para")[0]).find("button").removeAttr("disabled");
            $($(obj).find(".para")[0]).find("button").removeClass("layui-disabled");
            $("html,body").animate({scrollTop:$($(obj).find(".para")[0]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
        };

        // 进入条件if-then分支函数
        function ifthenFn(nextIndex) {
            //改变点击层次和点击数
            layerDM = 2;
            current = 0;
            dmSeqLengthFlag = $(nextIndex).index()+1;
            var dmSeqElement = $(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq");
            if($($(dmSeqElement).children(".dmNode")[0]).length>0){
                var dmNoede = $($(dmSeqElement).children(".dmNode")[0]);
                //修改步骤样式
                $($(dmNoede).find(".para")[0]).css("color","#333333");
                $($(dmNoede).find(".para")[0]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                $($(dmNoede).find(".para")[0]).find("button").removeAttr("disabled");
                $($(dmNoede).find(".para")[0]).find("button").removeClass("layui-disabled");
                $("html,body").animate({scrollTop:$($(dmNoede).find(".para")[0]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
            }
            //循环显示内容
            $.each($(dmSeqElement).children(".dmNode"), function (index, value) {
                if (!$(this).hasClass("hidden")) {
                    var steps = $(this).find(".proceduralStep");
                    $(steps[current]).removeClass("hidden");
                    //判断第一个是否需要显示name
                    if($(steps[current] ).find(".variableRef")){
                        //删除hidden，显示name
                        $(steps[current] ).find(".variableRef").removeClass("hidden");
                        $(steps[current] ).find(".variableRef").text(window.name);
                    }
                }
            })
        };

        // 正确流程函数
        function rightAnswerFn(obj) {
            layerDM = 1;
            current = 0;
            dmSeqLengthFlag = $(obj).index()+1;
            //修改步骤样式
            $($(obj).find(".para")[0]).css("color","#333333");
            $($(obj).find(".para")[0]).find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
            $($(obj).find(".para")[0]).find("button").removeAttr("disabled");
            $($(obj).find(".para")[0]).find("button").removeClass("layui-disabled");
            $("html,body").animate({scrollTop:$($(obj).find(".para")[0]).offset().top - $(window).height()/2 + 100 + "px"}, 500);
        };

        // 错误循环流程函数
        function wrongAnswerFn() {
            layerDM = 2;
            current = 0;
            var dmNodeAll = $(".dmSeq .dmIf .dmThenSeq .dmLoop .dmSeq").children(".dmNode");
            //循环所有dmNode标签
            $.each(dmNodeAll, function (index, value) {
                if(index==0){
                    $($($(this)[0]).find(".proceduralStep")[0]).find(".para").css("color","#333333");
                    $($($(this)[0]).find(".proceduralStep")[0]).find(".para").find("button").css({"border":"1px solid #186FF2","color":"#186FF2"});
                    $($($(this)[0]).find(".proceduralStep")[0]).find(".para").find("button").removeAttr("disabled");
                    $($($(this)[0]).find(".proceduralStep")[0]).find(".para").find("button").removeClass("layui-disabled");
                    $("html,body").animate({scrollTop:$($($(this)[0]).find(".proceduralStep")[0]).find(".para").offset().top - $(window).height()/2 + 100 + "px"}, 500);
                }
            });
        };

        // 错误循环流程--表达式拼接函数
        function wrongJudgmentAgeFn(entries,newArray) {
            var expressionValue = 1;
            newArray.forEach(function (value, index) {
                var children = $(newArray[index]).children();
                for (var i = 0; i < children.length; i++) {
                    if ($(children[i]).hasClass("menu")) {
                        var menuChoiceChildren = $(children[i]).children(".menuChoice");
                        for (var j = 0; j < menuChoiceChildren.length; j++) {
                            var menuChoice = $(menuChoiceChildren[j]).children(".prompt").text();
                            if( entries == menuChoice){
                                var assertionChildren = $(menuChoiceChildren[j]).children(".assertion");
                                //整个表达式的标签
                                var expressionElement = $($(assertionChildren)[1]).children(".expression");
                                //表达式拼接函数
                                expressionValue = expressionFn2(expressionElement);
                            }
                        }
                    }
                }
            });
            return expressionValue;
        };

        function expressionFn2(expressionElement) {
            //每失败一次，运行一次失败的表达式
            if (window.tourMistakes !== "undefined" && window.tourMistakes !== undefined) {
                var data1 = window.tourMistakes;
                var data2 = symbolicFn($(expressionElement).children(".numberOperation").text());
                var data3 = $($(expressionElement).children(".expression")[1]).text();
                window.tourMistakes = eval(data1 + data2 + data3);
            }else {
                window.tourMistakes = 1;
            }
            var tourMistakes = window.tourMistakes;
            return tourMistakes;
        }


    });
});


